package com.derbysoft.synxisadapter.dswitch2.service.impl;

import com.derbysoft.core.Limit;
import com.derbysoft.synxisadapter.commons.repository.ReservationRepository;
import com.derbysoft.synxisadapter.commons.repository.impl.ReservationCondition;
import com.derbysoft.synxisadapter.dswitch2.service.MonitorService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.UnsupportedEncodingException;

public class MonitorServiceImpl implements MonitorService {
    public static final int K = 1024;
    public static final int ONE_HUNDRED = 100;
    private static Logger logger = LoggerFactory.getLogger(MonitorServiceImpl.class);

    private int sizeOfMemoryTest = 4 * K * K;

    private ReservationRepository reservationRepository;

    @Override
    public void doTest() {
        doDatabaseTest();
        doMemoryTest();
        logger.info("All test OK!");
    }

    private void doMemoryTest() {
        logger.info("Begin memory test");
        byte[] bytes = new byte[sizeOfMemoryTest];
        try {
            logger.info("End memory test :" + new String(bytes, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            logger.error(e.getMessage());
        }
    }

    private void doDatabaseTest() {
        logger.info("Begin database test");
        reservationRepository.findByCondition(new ReservationCondition(), new Limit(0, ONE_HUNDRED));
        logger.info("End database test");
    }

    public void setReservationRepository(ReservationRepository reservationRepository) {
        this.reservationRepository = reservationRepository;
    }

    public void setSizeOfMemoryTest(int sizeOfMemoryTest) {
        this.sizeOfMemoryTest = sizeOfMemoryTest;
    }
}
